package leetcode

/**
 * https://leetcode.cn/problems/zigzag-conversion/description/
 */
class Solution_6 {

    /**
     * X Y
     * 0<= x <=
     */

    fun convert(s: String, numRows: Int): String {
        val n = numRows
        val len = s.length
        if (len == 1 || n == 1 ) return s
        val count = n - 1
        val str = StringBuilder()
        var index = 0
        for (y in 0 until n){
            if (y == 0){
                while (index < len){
                    str.append(s[index])
                    index += 2 * count
                }
                continue
            }
            if (y == count){
                index = count
                while (index < len){
                    str.append(s[index])
                    index += 2 * count
                }
                continue
            }
            index = 0
            while (index < len){
                val prev = index + y
                val next = index + 2 * count - y
                if (prev < len) str.append(s[prev])
                if (next < len) str.append(s[next])
                index += 2 * count
            }

        }
        return str.toString()
    }
}